package com.biejiaogroup.librarymanager.controller;


import com.biejiaogroup.librarymanager.entity.User;
import com.biejiaogroup.librarymanager.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Description: 注册
 * @Author: JesseChan
 * @Date: 2022/5/16
 */
@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
    private UserService registerService = new UserService();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String userName = request.getParameter("userName");
        String userPassword = request.getParameter("userPassword");

        if(registerService.isUserNameRegisted(userName))
        {
            //用户名已被注册
            request.setAttribute("registerResult", "用户名已被注册");
            request.getRequestDispatcher("/register.jsp").forward(request, response);
        }
        else
        {
            //该用户名可用
             User user = new User();
             user.setUserName(userName);
             user.setUserPassword(userPassword);
             registerService.register(user);
             response.sendRedirect("login.jsp");
        }

    }
}
